{***************************************************************
    SpyGestion  Copyright (C) 2008 Christophe KOALI <nature2gas@gmail.com>
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
****************************************************************}
unit frmVaisseauxEdit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Spin, modListeAttaqueVaisseauxInfos, modGIDtoSTR, modBaseType;


type
  TformVaisseauxEdit = class(TForm)
    ComboBox1: TComboBox;
    Label1: TLabel;
    SpinEdit1: TSpinEdit;
    Label2: TLabel;
    cmdCancel: TButton;
    cmdValider: TButton;
    procedure cmdValiderClick(Sender: TObject);
    procedure cmdCancelClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    FGidToStr: TCollectionGIDtoSTR;

    FCurrentVaisseauxList: PLAVaisseauxInfos;

    FListCombo: TList;
    FListSpin : TList;

    procedure InitCombo(combo: TComboBox);

    procedure PlaceInFormVL(pe: PLAVaisseauxInfos);
    procedure SaveFormInVL(pe: PLAVaisseauxInfos);


    procedure SetGidToStr(Valeur: TCollectionGIDtoSTR);
  public
    property GidToStr: TCollectionGIDtoSTR read FGidToStr write SetGidToStr;
    

    { Public declarations }
    procedure EditVaisseaux(vl: PLAVaisseauxInfos);

    procedure InitForm;
  end;

var
  formVaisseauxEdit: TformVaisseauxEdit;

implementation

{$R *.dfm}

uses gnugettext;

procedure TformVaisseauxEdit.SetGidToStr(Valeur: TCollectionGIDtoSTR);
begin
    FGidToStr := Valeur;
    InitForm;
end;

procedure TformVaisseauxEdit.FormCreate(Sender: TObject);
begin
    FGidToStr := nil;

    FListCombo := TList.Create;
    FListSpin  := TList.Create;
end;

procedure TformVaisseauxEdit.FormDestroy(Sender: TObject);
begin
    FListCombo.Free;
    FListSpin.Free;
end;


procedure TformVaisseauxEdit.SaveFormInVL(pe: PLAVaisseauxInfos);
var xCombo: TComboBox;
    xSpin : TSpinEdit;
    i, j, k: integer;
begin

    if FListCombo.Count <= CNT_LA_VAISSEAUX_TYPE_MAX_INDICE then begin
       // erreur
       exit;
    end;
    if FListSpin.Count <= CNT_LA_VAISSEAUX_TYPE_MAX_INDICE then begin
       // erreur
       exit;
    end;

    k := 0;
    for i := 0 to CNT_LA_VAISSEAUX_TYPE_MAX_INDICE do begin
       xCombo := FListCombo.Items[i];
       xSpin  := FListSpin.Items[i];


       if xSpin.Value > 0 then begin
          // vérifie qu'un type de vaisseau est sélectionné
          if xCombo.ItemIndex > 0 then begin
             // un type est choisi
             // obtient le gid du vaisseaux
             j := FGidToStr.GetGIDFromStr(xCombo.Text);

             if j > 1 then begin
                pe[k].gid := j;
                pe[k].quantite := xSpin.Value;
                Inc(k);
             end;

          end;
       end;

    end;


    for i := k to CNT_LA_VAISSEAUX_TYPE_MAX_INDICE do begin
       pe[k].gid      := -1;
       pe[k].quantite := -1;
    end;

end;

procedure TformVaisseauxEdit.PlaceInFormVL(pe: PLAVaisseauxInfos);
label lVide;
var xCombo: TComboBox;
    xSpin : TSpinEdit;
    i, j: integer;
begin

    if FListCombo.Count <= CNT_LA_VAISSEAUX_TYPE_MAX_INDICE then begin
       // erreur
       exit;
    end;
    if FListSpin.Count <= CNT_LA_VAISSEAUX_TYPE_MAX_INDICE then begin
       // erreur
       exit;
    end;


    for i := 0 to CNT_LA_VAISSEAUX_TYPE_MAX_INDICE do begin
        xCombo := FListCombo.Items[i];
        xSpin  := FListSpin.Items[i];

        if (pe[i].quantite < 1) or (pe[i].gid < 1) then begin
           // pas sélectionné
lVide:     xCombo.ItemIndex := 0;
           xSpin.Value := 0;
        end else begin

           j := xCombo.Items.IndexOf(FGidToStr.GetGIDStr(pe[i].gid));
           if j >= 0 then begin
              xCombo.ItemIndex := j;
              xSpin.Value := pe[i].quantite;
           end else begin
              goto lVide;
           end;

        end;

    end;
end;


procedure TformVaisseauxEdit.InitCombo(combo: TComboBox);
var i: integer;
    pe: PGIDtoSTR;
begin
    combo.Items.Clear;
    combo.Items.Add(_('(vide)'));

    if FGidToStr = nil then begin
       exit;
    end;

    for i := 0 to FGidToStr.Count - 1 do begin
       pe := FGidToStr.Items[i];

       if GetTypeOfGID(pe.GID) = CNT_ITEM_DESIGN_VAISSEAUX then begin
          combo.Items.Add(pe.Valeur);
       end;

    end;

    combo.ItemIndex := 0;

end;

procedure TformVaisseauxEdit.InitForm;
const ESPACEMENT = 5;
var xCombo: TComboBox;
    xSpin : TSpinEdit;
    i: integer;
    NbTotal: integer;
begin

    if FListCombo.Count > CNT_LA_VAISSEAUX_TYPE_MAX_INDICE then begin
       // déjà intialisé
       exit;
    end;
    if FListSpin.Count > CNT_LA_VAISSEAUX_TYPE_MAX_INDICE then begin
       // déjà intialisé
       exit;
    end;
    if FGidToStr = nil then begin
       exit;
    end;

    NbTotal := FGidToStr.GetNbElementOfThisType(CNT_ITEM_DESIGN_VAISSEAUX);

    if NbTotal = 0 then begin
       exit;
    end;


    for i := 0 to CNT_LA_VAISSEAUX_TYPE_MAX_INDICE do begin
       xCombo := TComboBox.Create(Self);
       xSpin  := TSpinEdit.Create(Self);

       FListCombo.Add(xCombo);
       FListSpin.Add(xSpin);

       

       xCombo.Height := 21;
       xCombo.Width  := 177;

       xCombo.Left   := 30;
       xCombo.Top    := 32 + (i * (xCombo.Height + 1 + ESPACEMENT));


       xSpin.Height := 22;
       xSpin.Width  := 65;

       xSpin.Left  := 240;
       xSpin.Top   := xCombo.Top;

       xSpin.MinValue := 0;
       xSpin.MaxValue := 999999;

       // affichage
       xCombo.Parent := Self;
       xSpin.Parent := Self;

       // intialise le contenu du combo
       InitCombo(xCombo);

    end;

end;

procedure TformVaisseauxEdit.cmdCancelClick(Sender: TObject);
begin
    Close;
end;

procedure TformVaisseauxEdit.cmdValiderClick(Sender: TObject);
begin
    SaveFormInVL(FCurrentVaisseauxList);
    Close;
end;

procedure TformVaisseauxEdit.EditVaisseaux(vl: PLAVaisseauxInfos);
begin
    PlaceInFormVL(vl);
    FCurrentVaisseauxList := vl;

    ShowModal;
end;

end.
